using System;
using System.Net;
using System.Net.Mail;
using Edge.Common.Utils;
using Edge.Common.Utils;

namespace Edge.Common.Mail.Impl
{
    public class SendMailEventArgs : EventArgs
    {
        private MailMessage mailMessage;


        public SendMailEventArgs(MailMessage mm)
        {
            this.mailMessage = mm;
        }


        public MailMessage MailMessage
        {
            get { return mailMessage; }
        }
    }

    public class SendMailService : ISendMailService
    {
        private bool initialized = false;
        private SmtpClient smtpClient = null;

        private int port = 0;
        private string host;
        private string login;
        private string passwrd;
        private bool enabled = true;
        private string defaultFromField;
        private string defaultFromName;

        public event EventHandler<SendMailEventArgs> PreSend;

        public int Port
        {
            get { return port; }
            set { port = value; }
        }

        public string Host
        {
            get { return host; }
            set { host = value; }
        }

        public string Login
        {
            get { return login; }
            set { login = value; }
        }

        public string Passwrd
        {
            get { return passwrd; }
            set { passwrd = value; }
        }

        public bool Enabled
        {
            get { return enabled; }
            set { enabled = value; }
        }

        public string DefaultFromName
        {
            get { return defaultFromName; }
            set { defaultFromName = value; }
        }

        public string DefaultFromField
        {
            get { return defaultFromField; }
            set { defaultFromField = value; }
        }

        public void Init()
        {
            if (StringUtils.IsBlankOrNull(host))
                throw new InvalidOperationException("Invalid host name");

            if (port > 0)
            {
                smtpClient = new SmtpClient(host, port);
            }
            else
            {
                smtpClient = new SmtpClient(host);
            }
            NetworkCredential credentials = new NetworkCredential(login, passwrd);
            smtpClient.Credentials = credentials;
        }

        public void Send(MailMessage mm)
        {
            if (mm != null)
            {
                if (!enabled)
                    return;

                if (PreSend != null)
                    PreSend(this, new SendMailEventArgs(mm));

                smtpClient.Send(mm);
            }
        }

        public void SendAsync(MailMessage mm)
        {
            if (!enabled)
                return;

            throw new NotImplementedException();
        }

        public string[] DefaultFrom()
        {
            string[] ret = new string[2];
            ret[0] = defaultFromField;
            ret[1] = defaultFromName;
            return ret;
        }

        protected void CheckInitialized()
        {
            if (!initialized)
                throw new InvalidOperationException("Mail servive not initilzed (call SendMailService.Init() method before)");
        }

    }
}